<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>内容管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/lobsteruiframe/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/lobsteruiframe/layuiadmin/style/admin.css" rel="stylesheet" />
    <script src="/lobsteruiframe/layuiadmin/layui/layui.js"></script>
    <script src="/jslib/jquery-3.4.1.min.js"></script>
    <script src="/jslib/lobster.js"></script>

    <script src="/lobsteruiframe/ueditor/ueditor.config.js"></script>
    <script src="/lobsteruiframe/ueditor/ueditor.all.js"></script>
    <script src="/lobsteruiframe/importword/mammoth.browser.min.js"></script>
    <script src="/lobsteruiframe/importword/importword.js"></script>
    <style>
        .sele {
            color: #009688;
            font-weight: bold;
            cursor: pointer;
            line-height: 36px;
        }
    </style>
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <form class="layui-form layui-card-body">
                <input type="hidden" name="CID" Id="CID" value="0" />
                <div class="layui-form-item">
                    <label class="layui-form-label">内容标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="Title" id="Title" required lay-verify="required" placeholder="请输入标题"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">简介</label>
                    <div class="layui-input-block">
                        <textarea class="layui-textarea" cols="3" name="Intraduce" id="Intraduce"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"><i style="color:red;">*&ensp; </i>文章图片</label>
                    <div class="layui-input-block">
                        <input type="hidden" id="PicPath" name="PicPath" value="">
                        <div class="layui-upload">
                            <div class="layui-inline">
                                <button type="button" class="layui-btn" id="btnUpload">选择图片</button><input
                                    class="layui-upload-file" type="file" accept="" name="file">
                                <i style="color:#999;">图片大小不超过1M</i>
                            
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" name="imgview" id="imgview"
                                        style="width:90px; height:90px" src="../image/service.jpg">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">导入文档</label>
                    <div class="layui-input-block">
                        <input id="document" type="file" class="layui-btn" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block">
                        <script id="content" style="width:100%;height:500px;" name="content" type="text/plain">
                        </script>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="display:none">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script type="text/plain" id="upload_ue">
    </script>
    <script>
        lobsterlayui.main(['index', 'layer', 'form', 'element'],{
                data: {
                    $layui: null,
                    ue: null,//ue编辑器
                    nid: 0,
                    cid: 0,
                    node: {},
                    modelCont: {},
                    modelArt: {},
                    issubmit: false,//是否提交中
                    workId: 0
                },
                pageload: function (right) {
                    this.parsejwt();
                    var query = location.search;
                    if (query) {
                        query = query.substr(1);
                        query.split('&').forEach(item => {
                            if (item.split('=')[0] == "nid") {
                                this.data.nid = parseInt(item.split("=")[1]);
                            } else if (item.split('=')[0] == "cid") {
                                this.data.cid = parseInt(item.split("=")[1]);
                            }

                        });
                    }
                    var self = this;
                    this.data.ue = UE.getEditor('content');
                    this.data.ue.ready(function () {
                        self.data.ue.execCommand('serverparam', {
                            'workId': self.data.workId,
                        });
                        self.data.ue.setShow();
                    });
                    if (this.data.cid) {
                        this.loadArt();
                    } else {
                        this.data.modelCont.NodeID = this.data.nid;
                    }

                    this.initEvent();

                    ImportWord.init("document", lobsterlayui.GetAppId(), lobsterlayui.GetWorkId(), (result) => {
                        //document.getElementById("output").value = result.value;
                        self.data.ue.ready(function () {
                            self.data.ue.setContent(result.value);
                        });
                    });
                },
                //初始化
                initEvent: function () {
                    var self = this;
                    //保存
                    layui.form.on('submit(formDemo)', function (data) {
                        return self.saveCont(data);
                    });

                    //图片

                    layui.upload.render({
                        elem: "#btnUpload",
                        size: 1024,
                        url: "/basic/v1/OSS/UploadFile",
                        data: {
                        },
                        done: function (res) {
                            if (res.code == 0) {
                                $('#imgview').attr("src", res.data.url); //图片链接（base64）
                                $("#PicPath").val(res.data.url);
                            }
                        }
                    });
                },

                //加载文章数据
                loadArt: function () {
                    lobsterlayui.GETAPI('tsAdmin.getarticle',{cid: this.data.cid}).then(retdata=>{
                        this.data.modelCont = retdata.data.cont;
                        this.data.modelArt = retdata.data.art;
                        this.bindData();
                    })
                },
                //绑定数据
                bindData: function () {
                    console.log(this.data.modelCont)
                    $("#Title").val(this.data.modelCont.Title);
                    $("#CID").val(this.data.modelCont.CID);
                    $("#U_Author").val(this.data.modelArt.U_Author);
                    $("#ContType").val(this.data.modelCont.ContType);
                    if (this.data.modelCont.IsIndex) {
                        $("#IsIndex").prop("checked", true);
                    }
                    if (this.data.modelCont.EnablePush == 1) {
                        $("#EnablePush").prop("checked", true);
                    }
                    if (this.data.modelCont.IsHot) {
                        $("#IsHot").prop("checked", true);
                    }
                    //预期发布
                    if (this.data.modelCont.IsExpectPublish) {
                        $("#IsExpectPublish").prop("checked", true);
                    }
                    //预期发布日期
                    if (this.data.modelCont.ExpectPublishTime != "" && this.data.modelCont.IsExpectPublish == true) {
                        $("#ExpectPublishTime").val(this.data.modelCont.ExpectPublishTime);
                    }
                    else {
                        //$("#ExpectPublishTime").val(this.data.modelCont.ExpectPublishTime);
                    }
                    if (this.data.modelCont.PicPath) {
                        $("#imgview").show().attr("src", this.data.modelCont.PicPath);

                    } else {
                        //$("#imgview").hide();
                    }
                    $("#PicPath").val(this.data.modelCont.PicPath);
                    $("#Intraduce").val(this.data.modelCont.Intraduce);
                    layui.form.render("checkbox");
                    this.data.ue.ready(a => {
                        this.data.ue.setContent(this.data.modelArt.U_Cont);
                    });

                },
                //保存内容
                saveCont: function (obj) {
                    console.log(obj)
                    var title = $("#Title").val();
                    var cont = this.data.ue.getContent();
                    cont = cont.trim();
                    this.data.modelCont.EnablePush = 0;
                    this.data.modelCont.IsIndex = false;
                    this.data.modelCont.IsHot = false;
                    this.data.modelCont.IsExpectPublish = false;
                    $.extend(this.data.modelCont, obj.field);
                    this.data.modelCont.Title = title;
                    this.data.modelArt.U_Cont = cont;
                    console.log(obj.field.U_Author)
                    this.data.modelArt.U_Author = obj.field.U_Author;

                    if (cont.length == 0) {
                        layer.msg("请输入内容");
                        return false;
                    }
                    if (this.data.issubmit) {
                        layer.msg("提交中")
                    }
                    this.data.issubmit = true;
                    var index = layer.load();
                    lobsterlayui.POSTAPI('tsAdmin.savecontext',{
                            Status: this.data.modelCont.Status,
                            CID: this.data.modelCont.CID,
                            Title: this.data.modelCont.Intraduce,
                            Intraduce: this.data.modelCont.Intraduce,
                            NodeID: this.data.modelCont.NodeID,
                            PicPath:this.data.modelCont.PicPath,
                            U_Cont: cont,
                        }).then(retdata=>{
                            layer.close(index);
                            parent.layer.closeAll();
                            layer.msg("保存成功");
                            if (window.submitSuccess) {
                                window.submitSuccess();
                            }
                            this.data.issubmit = false;
                        })
                    return false;
                },
                parsejwt: function () {
                    this.data.workId = lobsterlayui.GetWorkId();
                }

        })

    </script>
</body>

</html>